<?php
class DateTimeWidget implements Widget {
    private $dateFormat;
    private $timeFormat;
    private $at;
    
    public function __construct($dateFormat, $timeFormat, $at='@') {
        $this->dateFormat = $dateFormat;
        $this->timeFormat = $timeFormat;
        $this->at = $at;
    }
    
    public function interpretInput($input) {
        $format = $this->dateFormat . ' ' . $this->timeFormat;
        $value = $input['date'] . ' ' . $input['time'];
        return DateTime::createFromFormat($format, $value);
    }
    
    public function toHtml($name, $value, $id) {
        return sprintf('<input type="text" name="%s[date]" data-dateformat="%s" '.
                'value="%s"%s/> %s <input type="text" name="%s[time]" '.
                'value="%s"%s',
                $name,
                htmlspecialchars($this->dateFormat),
                $value !== null ? $value->format($this->dateFormat) : '',
                $id !== null ? ' id="'.$id.'-date"' : '',
                $this->at,
                $name,
                $value !== null ? $value->format($this->timeFormat) : '',
                $id !== null ? ' id="'.$id.'-time"' : '');
    }
}